"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '26/10/2024'
    comment: apollo工程相关
"""
import asyncio

from config import Config
from project.model.basic import Operator, User
from project.model.micro_service import MicroService
from project.core.exception import BizError


class Apollo:

    @staticmethod
    async def do_flow_interaction(operator: Operator, user_message, flow, is_init=True):
        """ 得到用户的登录信息
        :param operator:
        :param flow:
        :param user_message:
        :param is_init:
        :return:
        """
        data = await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/agent/flow/policy',
                                                                    user={'dev_id': 'dev_99', 'bot_id': 'udi', 'user_id': operator.code},
                                                                    user_message=user_message,
                                                                    flow=flow,
                                                                    is_init=is_init)

        return data

    @staticmethod
    async def init_user_memories(dev_id):
        """ 清除设备记忆
        :param dev_id:
        :return:
        """
        await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/agent/user/memories/init', dev_id=dev_id)

    @staticmethod
    async def add_user_memories(user: User, agent_profile, user_role, user_message='', agent_message=''):
        """
        :param user:
        :param agent_profile:
        :param user_role:
        :param user_message:
        :param agent_message:
        :return:
        """
        await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/agent/user/memories/add',
                                                             user=user.dict(),
                                                             user_message=user_message,
                                                             agent_message=agent_message,
                                                             agent_profile=agent_profile,
                                                             user_role=user_role)

    @staticmethod
    async def collect_advice(user: User, user_message):
        """ 搜集建议
        :param user:
        :param user_message:
        :return:
        """
        await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/agent/collect/advice', user=user.dict(), user_message=user_message)

    @staticmethod
    async def get_global_intent(user: User, user_message, flows, agent_profile, user_role, **kwargs):
        """
        :param user:
        :param user_message:
        :param flows:
        :param agent_profile:
        :param user_role:
        :return:
        """
        data = await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/agent/global/intent',
                                                                    user=user.dict(),
                                                                    user_message=user_message,
                                                                    flows=flows,
                                                                    agent_profile=agent_profile,
                                                                    user_role=user_role,
                                                                    **kwargs)
        return data

    @staticmethod
    async def init_radar_user_memories(dev_id):
        """ 清空毫米波雷达用户记忆
        :param dev_id:
        :return:
        """
        await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/radar/user/memories/init', dev_id=dev_id)

    @staticmethod
    async def do_radar_room_enquire(dev_id, room_info, user_message):
        """ 毫米波雷达问询相关
        :param dev_id:
        :param room_info: 房间信息
        :param user_message:
        :return:
        """
        data = await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/radar/enquire', dev_id=dev_id, room_info=room_info, user_message=user_message)

        return data

    @staticmethod
    async def do_knowledge_flush(knowledge_detail_code, organization_code, text):
        """ 知识库文本向量化
        :param knowledge_detail_code:
        :param organization_code:
        :param text:
        :return:
        """
        try:
            await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/knowledge/flush', knowledge_detail_code=knowledge_detail_code, organization_code=organization_code, text=text)
        except BizError:
            pass

    @staticmethod
    async def delete_knowledge(knowledge_detail_code):
        """ 删除知识库
        :param knowledge_detail_code:
        :return:
        """
        try:
            await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/knowledge/delete', knowledge_detail_code=knowledge_detail_code)
        except BizError:
            pass

    @staticmethod
    async def delete_knowledge_batch(knowledge_detail_codes):
        """ 删除知识库(批量)
        :param knowledge_detail_codes:
        :return:
        """
        try:
            await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/knowledge/delete/batch', knowledge_detail_codes=knowledge_detail_codes)
        except BizError:
            pass

    @staticmethod
    async def get_knowledge_match_list(knowledge_detail_codes, text, count=3):
        """ 得到知识库文本匹配列表
        :param knowledge_detail_codes:
        :param text:
        :param count:
        :return:
        """
        data = await MicroService(domain=Config.APOLLO_SERVER).post(url_path='/knowledge/match/list', knowledge_detail_codes=knowledge_detail_codes, text=text, count=count)

        return data

    @staticmethod
    async def do_agent_chat_streaming(knowledge_detail_codes, user_message, chat_messages):
        """ 智能体聊天(流式)
        :param knowledge_detail_codes:
        :param user_message:
        :param chat_messages:
        :return:
        """
        response_streaming = MicroService(domain=Config.APOLLO_SERVER).post_streaming(url_path='/knowledge/chat/streaming', knowledge_detail_codes=knowledge_detail_codes, user_message=user_message, chat_messages=chat_messages)

        return response_streaming


async def main():
    async for stream in await Apollo.do_agent_chat_streaming(['1737082360532311', '1737082360358785'], '酒店有送餐上门服务吗？', []):
        print(stream)

if __name__ == "__main__":

    asyncio.run(main())

